from PySide6.QtWidgets import QApplication,QWidget,QPushButton,QGridLayout,QLineEdit
import sys

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.lineEdit = QLineEdit()
        self.bind()
        # 临时存储计算过程
        self.result =''
    def bind(self):
        # 网格布局
        grid = QGridLayout()

        # 输入框
        self.lineEdit.setPlaceholderText('请输入你要计算的数字')
        self.lineEdit.setStyleSheet("QLineEdit { height: 40px; }")
        # 计算按钮
        btn_7 = QPushButton('7')
        btn_8 = QPushButton('8')
        btn_9 = QPushButton('9')
        btn_ = QPushButton('/')
        # 第二排
        btn_4 = QPushButton('4')
        btn_5 = QPushButton('5')
        btn_6 = QPushButton('6')
        btn_x = QPushButton('x')
        # 第三排
        btn_1 = QPushButton('1')
        btn_2 = QPushButton('2')
        btn_3 = QPushButton('3')
        btn__ = QPushButton('-')
        # 第四拍
        btn_0 = QPushButton('0')
        btn_point = QPushButton('.')
        btn_cal = QPushButton('计算')
        btn_add = QPushButton('+')

        # 添加布局
        grid.addWidget(self.lineEdit, 0, 0, 3, 4)

        grid.addWidget(btn_7, 4, 0)
        grid.addWidget(btn_8, 4, 1)
        grid.addWidget(btn_9, 4, 2)
        grid.addWidget(btn_, 4, 3)

        grid.addWidget(btn_4, 5, 0)
        grid.addWidget(btn_5, 5, 1)
        grid.addWidget(btn_6, 5, 2)
        grid.addWidget(btn_x, 5, 3)

        grid.addWidget(btn_1, 6, 0)
        grid.addWidget(btn_2, 6, 1)
        grid.addWidget(btn_3, 6, 2)
        grid.addWidget(btn__, 6, 3)

        grid.addWidget(btn_0, 7, 0)
        grid.addWidget(btn_point, 7, 1)
        grid.addWidget(btn_cal, 7, 2)
        grid.addWidget(btn_add, 7, 3)
        self.setLayout(grid)

        # 绑定信号槽
        btn_7.clicked.connect(lambda:self.addNumber('7'))
        btn_8.clicked.connect(lambda:self.addNumber('8'))
        btn_9.clicked.connect(lambda:self.addNumber('9'))
        btn_.clicked.connect(lambda:self.addNumber('/'))
        btn_4.clicked.connect(lambda:self.addNumber('4'))
        btn_5.clicked.connect(lambda:self.addNumber('5'))
        btn_6.clicked.connect(lambda:self.addNumber('6'))
        btn_x.clicked.connect(lambda:self.addNumber('*'))
        btn_1.clicked.connect(lambda:self.addNumber('1'))
        btn_2.clicked.connect(lambda:self.addNumber('2'))
        btn_3.clicked.connect(lambda:self.addNumber('3'))
        btn__.clicked.connect(lambda:self.addNumber('-'))
        btn_0.clicked.connect(lambda:self.addNumber('0'))
        btn_point.clicked.connect(lambda:self.addNumber('.'))
        btn_add.clicked.connect(lambda:self.addNumber('+'))

        # 计算
        btn_cal.clicked.connect(lambda:self.equal())

    # 计算传递过来的值
    def addNumber(self,num):
        self.lineEdit.clear()
        self.result +=num
        self.lineEdit.setText(self.result)

    def equal(self):
        self.lineEdit.clear()
        number=eval(self.result)
        self.lineEdit.setText(str(number))



if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWidget()
    window.setWindowTitle('计算器')
    window.show()
    sys.exit(app.exec())